package yl.hs.bmipfull.service.chart.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.finance.IOtherExpensesRecordDao;
import yl.hs.bmipfull.daos.finance.IReceiptSettleMainDao;
import yl.hs.bmipfull.daos.finance.IReceivePaymentRecordDao;
import yl.hs.bmipfull.daos.finance.IShipSettleMainDao;
import yl.hs.bmipfull.daos.laboratory.IProductCateDao;
import yl.hs.bmipfull.daos.laboratory.IProductInfoDao;
import yl.hs.bmipfull.daos.material.IPurchaseReceiptDao;
import yl.hs.bmipfull.daos.material.IRawMaterialCateDao;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.pojo.finance.ShipSettleMainEntity;
import yl.hs.bmipfull.pojo.production.ProductShipOrderEntity;
import yl.hs.bmipfull.pojo.salemanage.SaleContractEntity;
import yl.hs.bmipfull.service.chart.IFinanceChatService;
import yl.hs.bmipfull.service.finance.IReceivePaymentRecordService;
import yl.hs.bmipfull.service.finance.IShipSettleService;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.salemanage.ISaleContractService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.LoginUser;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 谢东
 * @apiNote
 * @since 2022-03-17 9:08 创建
 */
@Service
public class FinanceChatServiceImpl implements IFinanceChatService {

    @Autowired
    IOtherExpensesRecordDao otherExpensesRecordDao;
    @Autowired
    IProductShipOrderDao productShipOrderDao;
    @Autowired
    IProductCateDao productCateDao;
    @Autowired
    IPurchaseReceiptDao purchaseReceiptDao;
    @Autowired
    IReceivePaymentRecordDao receivePaymentRecordDao;
    @Autowired
    IShipSettleMainDao shipSettleMainDao;
    @Autowired
    IReceiptSettleMainDao receiptSettleMainDao;

    @Autowired
    IRawMaterialCateDao rawMaterialCateDao;

    @Autowired
    ISaleContractService saleContractService;

    @Autowired
    IShipSettleService shipSettleService;

    @Autowired
    IReceivePaymentRecordService paymentRecordService;

    @Autowired
    IProductInfoDao productInfoDao;



    @Override
    public Integer getProductShipOrderNotApproved() {
        return productShipOrderDao.getNotApproved();
    }

    @Override
    public Integer getPurchaseReceiptNotApproved() {
        return purchaseReceiptDao.getNotApproved();
    }

    @Override
    public BigDecimal getSumTradeAmountByDate(int tradeType, int dateType) {
        return receivePaymentRecordDao.getSumTradeAmountByDate(tradeType, dateType);
    }

    @Override
    public BigDecimal getSumOtherAmount(int dateType) {
        return otherExpensesRecordDao.getSumAmountByDate(dateType);
    }

    @Override
    public BigDecimal getSettleAccountReceivable() {
        BigDecimal collection=receivePaymentRecordDao.getSumTradeAmountByTradeType(1);
        BigDecimal settlement=shipSettleMainDao.getSumCurrentAmount();
        return settlement.multiply(collection);
    }

    @Override
    public BigDecimal getSettlementPayable() {
        BigDecimal payment=receivePaymentRecordDao.getSumTradeAmountByTradeType(0);
        BigDecimal settlement=receiptSettleMainDao.getSumCurrentAmount();
        return settlement.multiply(payment);
    }

    @Override
    public BigDecimal getExamineQuantity() {
        return productShipOrderDao.getExamineQuantity();
    }

    @Override
    public Map<String, Object> getReceivableAndNetReceiptChart() {
        Map<String,Object> result=new HashMap<>();
        List<String> legend=new ArrayList<>();
        List<BigDecimal> dataA=new ArrayList<>();
        List<BigDecimal> dataB=new ArrayList<>();
        List<BigDecimal> dataC=new ArrayList<>();
        List<String> xAxis=new ArrayList<>();
        legend.add("应收金额");
        legend.add("已收金额");
        legend.add("回款比例");
        result.put("legend",legend);
        var cateList=productCateDao.getAll();
        for (var cate : cateList ) {
            xAxis.add(cate.getCateName());
            var yingShou=shipSettleMainDao.getSumCurrentAmountByCateSN(cate.getSn());
            var htSNList=productShipOrderDao.getContractSNByCateSN(cate.getSn());
            var shiShou=BigDecimal.ZERO;
            if(htSNList.size()>0){
                shiShou=receivePaymentRecordDao.getSumTradeAmountByContractSN(htSNList.toArray(new String[htSNList.size()]));
            }
            dataA.add(yingShou);
            dataB.add(shiShou);
            if(shiShou.compareTo(BigDecimal.ZERO)==0||yingShou.compareTo(BigDecimal.ZERO)==0){
                dataC.add(BigDecimal.ZERO);
            }else{
                dataC.add(shiShou.divide(yingShou, RoundingMode.CEILING).multiply(new BigDecimal(100)));
            }

        }
        result.put("dataA",dataA);
        result.put("dataB",dataB);
        result.put("dataC",dataC);
        result.put("xAxis",xAxis);
        return result;
    }

    @Override
    public Map<String, Object> getActualPaymentPayableChart() {
        Map<String,Object> result=new HashMap<>();
        List<String> legend=new ArrayList<>();
        List<BigDecimal> dataA=new ArrayList<>();
        List<BigDecimal> dataB=new ArrayList<>();
        List<BigDecimal> dataC=new ArrayList<>();
        List<String> xAxis=new ArrayList<>();
        legend.add("应付金额");
        legend.add("已付金额");
        legend.add("付款比例");
        result.put("legend",legend);
        var cateList=rawMaterialCateDao.getAll();
        for (var cate : cateList ) {
            xAxis.add(cate.getCateName());
            var htSNList=purchaseReceiptDao.getContractSNByCateSN(cate.getSn());
            String[] htSnStr=htSNList.toArray(new String[htSNList.size()]);
            var yingFu=BigDecimal.ZERO;
            var shiFu=BigDecimal.ZERO;
            if(htSNList.size()>0){
                shiFu=receivePaymentRecordDao.getSumTradeAmountByContractSN(htSnStr);
                yingFu=receiptSettleMainDao.getSumCurrentAmountByContractSN(htSnStr);
            }
            dataA.add(yingFu);
            dataB.add(shiFu);
            if(shiFu.compareTo(BigDecimal.ZERO)==0||yingFu.compareTo(BigDecimal.ZERO)==0){
                dataC.add(BigDecimal.ZERO);
            }else{
                dataC.add(shiFu.divide(yingFu).multiply(new BigDecimal(100)));
            }

        }
        result.put("dataA",dataA);
        result.put("dataB",dataB);
        result.put("dataC",dataC);
        result.put("xAxis",xAxis);
        return result;
    }

    @Override
    public BigDecimal getExamineNetWeight() {
        return purchaseReceiptDao.getExamineNetWeight();
    }

    @Override
    public BigDecimal getCurrentUserCollectionAmount() {
        LoginUser current = HsApp.getCurrent();
        BigDecimal result = BigDecimal.ZERO;
        if (current != null) {
            List<SaleContractEntity> contractList = saleContractService.queryBySaleSn(current.getUserSN());
            for (SaleContractEntity contract : contractList) {
                ShipSettleMainEntity shipSettleMain = shipSettleService.findLastByContractSn(contract.getSn());
                BigDecimal receivedAmount = paymentRecordService.sumAmountByContractSn(contract.getSn());
                if (shipSettleMain != null) {
                    result = result.add(shipSettleMain.getTotalAmount().multiply(receivedAmount));
                } else{
                    var shipOrderList = productShipOrderDao.queryByContractSn(contract.getSn());

                    for (var shipOrder : shipOrderList) {
                        BigDecimal quantity;
                        if (!Tl.isEmpty(shipOrder.getShipmentReviewFlag())){
                            quantity = shipOrder.getShipmentReviewQuantity();
                        } else if (!Tl.isEmpty(shipOrder.getSigner())){
                            quantity = shipOrder.getSignedQuantity();
                        } else {
                            quantity = shipOrder.getQuantity();
                        }

                        BigDecimal amount = shipOrder.getPrice();
                        result = result.add(amount != null?amount.multiply(quantity==null?BigDecimal.ZERO:quantity):BigDecimal.ZERO);
                    }
                }
            }
        }
        return result;
    }
}
